<template>
  <div>
    <el-autocomplete
      popper-class="my-autocomplete"
      v-model="state"
      :fetch-suggestions="querySearch"
      placeholder="请输入内容"
      @select="handleSelect">
      <i
        class="el-icon-edit el-input__icon"
        slot="suffix"
        @click="handleIconClick">
      </i>
      <template slot-scope="{ item }">
        <div class="imgDiv">
          <img src="../assets/img/patient.png" class="image">
        </div> 
        <div class="wordDiv">
        <span class="name">{{ item.name }}</span>
        <span class="addr">{{ item.sex }}</span>
        <span class="addr">{{ item.age }}岁</span>
        </div> 
        <br/>
        <span class="addr">{{ item.phoneNumber }}</span>
      </template>
    </el-autocomplete>
  </div>
</template>

<script>
import axios from 'axios'

  export default {
    props:['text'],
    watch: {
      text:{
        deep: true,
        handler(newValue, oldValue) {
        this.state = newValue;
        },
      }
    },
    data() {
      return {
        restaurants: [],
        state: '',
      };
    },
    methods: {
      querySearch(queryString, cb) {
        var restaurants = this.restaurants;
        var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
        console.log("results",results)
        // 调用 callback 返回建议列表的数据
        cb(results);
      },
      createFilter(queryString) {
        return (restaurant) => {
          return (restaurant.name.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
        };
      },
      handleSelect(item) {
        this.state=item.name;
        this.$emit('callFather',item);
      },
      handleIconClick(ev) {
        console.log(ev);
      },
      getdata(){
        axios.post('/api/patient/findAll',{  "pageNum":1, "pageSize":1000})
        .then(res => {
          console.log("res",res)
           this.restaurants= res.data.data.list 
         })
         .catch(err => {
          this.$message.error('患者列表加载失败，请稍后再试！')
         })
      }
    },
    created(){
      this.getdata()
    }
  }
</script>

<style>
.image {
    width: 53px;
  }
.imgDiv{
  float:left;
  width:55px;
}
.wordDiv{
  float:right;
  width:100px;
}
.name{
  font-weight:900;
  font-size:14px;
}
.my-autocomplete {
  li {
    line-height: normal;
    padding: 7px;

    .name {
      text-overflow: ellipsis;
      overflow: hidden;
      font-weight:60;
    }
    .addr {
      font-size: 12px;
      color: #b4b4b4;
    }
    .highlighted .addr {
      color: #ddd;
    }
  }
}
</style>